﻿#define MySql
#undef MSSql

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Text.RegularExpressions;

using MySql.Data.MySqlClient;

namespace ukeasbjWeb
{

    public partial class LeadEdit : Bonus
    {
        
        //合同主键
        string sSeries_No_Lead;
        //已有的前台
        //static string Existing_Name_Receptionists;
        //已有的客服
        //static string Existing_Name_Frontline_Agent;

        static bool bOn_Frontline_Agent = false;
        static bool bOn_Receptionist = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            Common_SXD.Is_LongIn(Response, Session, Request);

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);
            bool bIs_Marketing = Convert.ToBoolean(Session["Is_Marketing"]);

            
            if (!bIs_O_M && !bIs_Admin && !bIs_Receptionist && !bIs_Counsellor && !bIs_Frontline_Agent
                && !bIs_Marketing)
            { //只有地区经理、系统管理员、前台、收款、市场专员和客服专员可以进入
                Response.Write("<h2 align='center'><font face='宋体' color='red'>");
                Response.Write("<br>对不起，请用其他身份登录！");
                Response.Write("</fong></h2>");
                Response.End();
            }

            if (!IsPostBack)
            {
                // 载入客户留学意向表，绑定到 Drop_Overseas_Study 中
                Common_SXD.Init_DropDownList(Drop_Overseas_Study, "Overseas_Study",
                    "select * from Overseas_Study  order by Code_Overseas_Study asc");

                // 载入客户接触方式表，绑定到 Drop_Type_Contact 中
                Common_SXD.Init_DropDownList(Drop_Type_Contact, "Type_Contact",
                    "select * from Type_Contact order by Code_Type_Contact asc");

                // 载入客户了解渠道表，绑定到 Drop_Channel_Customer2Know 中
                Common_SXD.Init_DropDownList(Drop_Channel_Customer2Know, "Channel_Customer2Know",
                    "select * from Channel_Customer2Know order by Code_Channel_Customer2Know asc");


                // 载入客户状态表，绑定到 Drop_State_Customer 中
                Common_SXD.Init_DropDownList(Drop_State_Customer, "State_Customer",
                    "select * from State_Customer order by Code_State_Customer asc");

                // 载入二次分配客户状态表，绑定到 Drop_Second_State_Customer 中
                Common_SXD.Init_DropDownList(Drop_Second_State_Customer, "State_Customer",
                    "select * from State_Customer order by Code_State_Customer asc");
            }

            ///////////// 读出合同主键
            sSeries_No_Lead = Request["Series_No_Lead"];
            Session["Flag"] = sSeries_No_Lead;
            ///////////// 分离加在合同主键前部的控制字符
            Regex NotEditFirstPayment = new Regex("([a-zA-Z_]{1,}):([0-9]{1,})");
            Regex NewOne = new Regex("(NewOne)");
            Regex EditFirstPayment = new Regex("([0-9]{1,})");
            string[] temp;

            if (NotEditFirstPayment.IsMatch(sSeries_No_Lead))
            {
                temp = NotEditFirstPayment.Split(sSeries_No_Lead);
                if (temp[1] == "ReadOnly")
                {// 只读方式读取合同数据
                    sSeries_No_Lead = temp[2];
                    Label_Msg.Text = sSeries_No_Lead;
                    if (!IsPostBack)
                        Init_ReadOnly();
                    
                    return;
                }
                else if (temp[1] == "ReplyFromCounsellor")
                {// 
                    sSeries_No_Lead = temp[2];
                    Label_Msg.Text = sSeries_No_Lead;
                    if (!IsPostBack)
                        Init_ReplyFromCounsellor();
                    
                    return;
                }
                else if (temp[1] == "ReplyFromFrontlineAgent")
                {
                    sSeries_No_Lead = temp[2];
                    Label_Msg.Text = sSeries_No_Lead;
                    if (!IsPostBack)
                        Init_ReplyFromFrontlineAgent();
                    
                    return;

                }
                else if (temp[1] == "ReplyFromO_M")
                {
                    sSeries_No_Lead = temp[2];
                    Label_Msg.Text = sSeries_No_Lead;
                    if (!IsPostBack)
                        Init_ReplyFromO_M();
                    
                    return;

                }
                else if (temp[1] == "Continue_Follow_up")
                {
                    sSeries_No_Lead = temp[2];
                    Label_Msg.Text = sSeries_No_Lead;
                    if (!IsPostBack)
                        Init_Continue_Follow_up();
                    
                    return;

                }
                else if (temp[1] == "EditReceptionist")
                {
                    sSeries_No_Lead = temp[2];
                    Label_Msg.Text = sSeries_No_Lead;
                    if (!IsPostBack)
                        Init_EditReceptionist();
                    
                    return;

                }
                else if (temp[1] == "EditMarketing")
                {
                    sSeries_No_Lead = temp[2];
                    Label_Msg.Text = sSeries_No_Lead;
                    if (!IsPostBack)
                        Init_EditMarketing();
                    
                    return;

                }
            }

            if (!IsPostBack)
            {

                ///if (NewOneFirstPayment.IsMatch(sSeries_No))
                ///{
                    ///Label_Msg.Text = sSeries_No;
                    Init_NewOne(); //初始化：添加新客户数据页面
                    Label_Msg.Text = sSeries_No_Lead;
                    return;
                ///}
               
            }

        }

        
        
        

        /// <summary>
        /// 入读leads数据表的内容，到页面控件。
        /// </summary>
        /// <param name="mySql"></param>
        /// <param name="myResponse"></param>
        /// <returns></returns>
        private void ReadAllFromLeads(DataRow myrowUser,
             HttpResponse myResponse)
        {/*
            SqlConnection sqlCnn = new SqlConnection(strCnn);
            SqlDataAdapter sqlDA = new SqlDataAdapter(mysSql, sqlCnn);
            sqlDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            DataSet bsDataSet = new DataSet();
            sqlDA.Fill(bsDataSet, "Leads");
            if (bsDataSet.Tables["Leads"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Leads"].Rows[0]; // 只有一行
            */   
            // 显示用户信息
                if (TextBox_Office.Visible)
                {
                    TextBox_Office.Text = myrowUser["Office"].ToString().Trim();
                    ///TextBox_Office.Text = myrowUser["Office_Leads_Selected"].ToString().Trim();
                    
                }///InitializeDrop_Writer(TextBox_Office.Text);

                if (TextBox_Date_Register.Visible)
                {
                    TextBox_Date_Register.Text = myrowUser["Date_Register"].ToString().Trim();
                    TextBox_Date_Register.Text = Common_SXD.TimeToYMD(TextBox_Date_Register.Text);
                }
                if (TextBox_Name_Inquirer.Visible)
                {
                    TextBox_Name_Inquirer.Text = myrowUser["Name_Inquirer"].ToString().Trim();
                }
                if (TextBox_Tel_Inquirer.Visible)
                {
                    TextBox_Tel_Inquirer.Text = myrowUser["Tel_Inquirer"].ToString().Trim();
                }
                if (TextBox_Email_Inquirer.Visible)
                {
                    TextBox_Email_Inquirer.Text = myrowUser["Email_Inquirer"].ToString().Trim();
                }
                if (TextBox_QQMSN_Inquirer.Visible)
                {
                    TextBox_QQMSN_Inquirer.Text = myrowUser["QQMSN_Inquirer"].ToString().Trim();
                }
                if (Drop_Overseas_Study.Visible)
                {
                    Common_SXD.SetSelectedIndex(myrowUser["Overseas_Study"].ToString().Trim(), Drop_Overseas_Study);
                }

                if (Drop_Type_Contact.Visible)
                {
                    Common_SXD.SetSelectedIndex(myrowUser["Type_Contact"].ToString().Trim(), Drop_Type_Contact);
                }
///                if (Drop_FromWeb.Visible)
///                {
///                    Common_SXD.SetSelectedIndex(myrowUser["FromWeb"].ToString().Trim(), Drop_FromWeb);
///                }
                if (CheckBox_Is_New.Visible)
                {
                    if (myrowUser["Is_New"].ToString().Trim() == "True" || myrowUser["Is_New"].ToString().Trim() == "1")
                        CheckBox_Is_New.Checked = true;
                    else
                        CheckBox_Is_New.Checked = false;
                }
            /*
                if (CheckBox_Is_Active.Visible)
                {
                    string temp = myrowUser["Is_Active"].ToString().Trim();
                    //if (myrowUser["Is_Active"].ToString().Trim() == "True" || myrowUser["Is_New"].ToString().Trim() == "1")
                    if (temp == "True" || temp  == "1")
                            CheckBox_Is_Active.Checked = true;
                    else
                        CheckBox_Is_Active.Checked = false;
                }
            */
                Read_CheckBox(CheckBox_Is_Active, "Is_Active", myrowUser);
                if (Drop_Channel_Customer2Know.Visible)
                {
                    Common_SXD.SetSelectedIndex(myrowUser["Channel_Customer2Know"].ToString().Trim(), Drop_Channel_Customer2Know);
                }
                if (TextBox_Notes_Lead.Visible)
                {
                    TextBox_Notes_Lead.Text = myrowUser["Notes_Lead"].ToString().Trim();
                }
                if (TextBox_Name_Receptionist.Visible)
                {
                    TextBox_Name_Receptionist.Text = myrowUser["Name_Receptionist"].ToString().Trim();
                }
                if (TextBox_Counsellor.Visible)
                {
                    TextBox_Counsellor.Text = myrowUser["Counsellor"].ToString().Trim();
                }
                if (Drop_Counsellor.Visible)
                {
                    Common_SXD.SetSelectedIndex(myrowUser["Counsellor"].ToString().Trim(), Drop_Counsellor);
                }               
                if (TextBox_Student_Name.Visible)
                {
                    TextBox_Student_Name.Text = myrowUser["Student_Name"].ToString().Trim();
                }
                if (TextBox_Tel_Student.Visible)
                {
                    TextBox_Tel_Student.Text = myrowUser["Tel_Student"].ToString().Trim();
                }
                if (Drop_State_Customer.Visible)
                {
                    Common_SXD.SetSelectedIndex(myrowUser["State_Customer"].ToString().Trim(), Drop_State_Customer);
                }




                if (TextBox_Date_First_Payment.Visible)
                {
                    TextBox_Date_First_Payment.Text = myrowUser["Date_First_Payment"].ToString().Trim();
                    TextBox_Date_First_Payment.Text = Common_SXD.TimeToYMD(TextBox_Date_First_Payment.Text);
                
                }
                if (TextBox_Series_No.Visible)
                {
                    TextBox_Series_No.Text = myrowUser["Series_No"].ToString().Trim();
                }
                if (TextBox_Name_Frontline_Agent.Visible)
                {
                    TextBox_Name_Frontline_Agent.Text = myrowUser["Name_Frontline_Agent"].ToString().Trim();
                }
                if (TextBox_Date_FA_Return_Visit.Visible)
                {
                    TextBox_Date_FA_Return_Visit.Text = myrowUser["Date_FA_Return_Visit"].ToString().Trim();
                    TextBox_Date_FA_Return_Visit.Text = Common_SXD.TimeToYMD(TextBox_Date_FA_Return_Visit.Text);

                }

                if (Drop_Second_State_Customer.Visible)
                {
                    Common_SXD.SetSelectedIndex(myrowUser["Second_State_Customer"].ToString().Trim(), Drop_Second_State_Customer);
                }
                if (TextBox_Second_Counsellor.Visible)
                {
                    TextBox_Second_Counsellor.Text = myrowUser["Second_Counsellor"].ToString().Trim();
                }


                if (Drop_Second_Counsellor.Visible)
                {
                    Common_SXD.SetSelectedIndex(myrowUser["Second_Counsellor"].ToString().Trim(), Drop_Second_Counsellor);
                }
                if (TextBox_Date_Continue_Follow_up.Visible)
                {
                    TextBox_Date_Continue_Follow_up.Text = myrowUser["Date_Continue_Follow_up"].ToString().Trim();
                    TextBox_Date_Continue_Follow_up.Text = Common_SXD.TimeToYMD(TextBox_Date_Continue_Follow_up.Text);
                }
                
                
           /// }
           /// sqlCnn.Close();

        }


        /// <summary>
        /// 用add_notes_lead 中的内容，添加在注释后面
        /// </summary>
        /// <param name="mySql"></param>
        /// <param name="myResponse"></param>
        /// <returns></returns>
        private string UpdatingNotes(string mySql,/* string myNotes,*/ HttpResponse myResponse)
        {

            string sNotes = TextBox_Notes_Lead.Text.Trim();
            sNotes = sNotes.Trim();
            Regex Deduct = new Regex("(Deduct)|(deduct)");
            Regex CommaDeduct = new Regex("([,]*[ ]*Deduct)|([，]*[ ]*Deduct)");
/*
            ////设定填写退款的判断范围
            Common_SXD.Set_BonusDate(Common_SXD.TimeToYM(TextBox_Date_Termination_Refund.Text.Trim()), true);

            if (TextBox_Add_Notes_Lead.Text.Trim().Length > 0)///判断Add_Notes
            {
                string temp0 = TextBox_Add_Notes_Lead.Text.Trim();

                // 注释中有内容，根据添加字符串的首字符判断
                if (TextBox_Notes_Lead.Text.Trim().Length > 0)
                {
                    //	string temp0 =	TextBox_Add_Notes.Text.Trim();
                    char temp1 = temp0[0];
                    if (temp1 == ',') // 首字符为逗号，直接添加；
                        sNotes += temp0;
                    else if (temp1 == '，')
                        sNotes += temp0;
                    else // 首字符不是逗号， + “ “ + TextBox_Add_Notes.Text.Trim();
                        sNotes += " " + temp0;
                }
                else // 注释中无内容，直接添加
                {
                    sNotes += temp0;
                }
                mySql += " Notes_Lead = '" + sNotes + "', ";
                Response.Write("<li>已经添加了注释，需要关闭窗口，刷新，才能看到添加的结果！</li>");
            }
*/
            return mySql;
        }

        /// <summary>
        /// 判断顾问回复错误
        /// </summary>
        public bool IsWrong_ReplyFromCousellor()
        {
/*            //不合理数据
            double Amount = Convert.ToDouble(TextBox_Amount.Text);
            double First_Payment = Convert.ToDouble(TextBox_First_Payment.Text);
            double Second_Payment = Convert.ToDouble(TextBox_Second_Payment.Text);

            if (Amount < (First_Payment + Second_Payment))
            {
                Response.Write("<li>合同金额与付款数据有错， 不能保存！</li>");
                return true;
            }
            /// 注释中不能出现 "SH" ,不能出现“SHANGHAI”
            string Notes = Convert.ToString(TextBox_Notes.Text);
            string Add_Notes = Convert.ToString(TextBox_Add_Notes.Text);
            //// Regex SH_SHANGHAI = new Regex("(Shanghai)|(SH[^\\s+])", RegexOptions.IgnoreCase);
            Regex SH_SHANGHAI = new Regex("(Shanghai)", RegexOptions.IgnoreCase);
            if (SH_SHANGHAI.IsMatch(Notes) || SH_SHANGHAI.IsMatch(Add_Notes))
            {
                Response.Write("<li>注释中不能出现 “Shanghai”！不能保存！</li>");
                return true;
            }
*/
            return true;
        }

        /////////////////////////////
        /// <summary>
        /// 更新合同数据库的记录（不是新数据）。
        /// </summary>
        /// <remarks>
        /// 只有修改数据的控件有效（enable）时，该数据才能被更新。
        /// </remarks>
        public void Update_Lead()
        {/*
            //不合理收费数据与合同金额数据
            if (Is_WrongPayment())
                return;
*/
            //客户信息写入数据库
            string sSql = "Update Leads Set";

///            if (TextBox_Office.Enabled)
///                sSql += " Office = '" + TextBox_Office.Text.Trim() + "',";

            if (TextBox_Date_Register.Enabled)
                sSql += " Date_Register = '" + TextBox_Date_Register.Text.Trim() + "', ";

            
            ///if (TextBox_Name_Inquirer.Enabled)
            ///    sSql += " Name_Inquirer = '" + TextBox_Name_Inquirer.Text.Trim() + "', ";

            sSql = sSql_Add_TextBox(TextBox_Name_Inquirer, "Name_Inquirer", sSql);
            
            if (TextBox_Tel_Inquirer.Enabled)
                sSql += " Tel_Inquirer = '" + TextBox_Tel_Inquirer.Text.Trim() + "', ";

            if (TextBox_Email_Inquirer.Enabled)
                sSql += " Email_Inquirer = '" + TextBox_Email_Inquirer.Text.Trim() + "', ";

            if (TextBox_QQMSN_Inquirer.Enabled)
                sSql += " QQMSN_Inquirer = '" + TextBox_QQMSN_Inquirer.Text.Trim() + "', ";

            if (Drop_Overseas_Study.Enabled)
                sSql += " Overseas_Study = '" + Drop_Overseas_Study.SelectedItem.Text.Trim() + "', ";



            if (Drop_Type_Contact.Enabled)
                sSql += " Type_Contact = '" + Drop_Type_Contact.SelectedItem.Text.Trim() + "', ";
           
///            if (Drop_FromWeb.Enabled)
///                sSql += " FromWeb = '" + Drop_FromWeb.SelectedItem.Text.Trim() + "', ";
            /*
            string temp;
            if (CheckBox_Is_New.Enabled)
            {
                if (CheckBox_Is_New.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_New = '" + temp + "', ";
            }
            
            if (CheckBox_Is_Active.Enabled)
            {
                if (CheckBox_Is_Active.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Active = '" + temp + "', ";
            }
            */
            sSql = sSql_Add_Checkd(CheckBox_Is_New, "Is_New", sSql);
            sSql = sSql_Add_Checkd(CheckBox_Is_Active, "Is_Active", sSql);

            if (Drop_Channel_Customer2Know.Enabled)
                sSql += " Channel_Customer2Know = '" + Drop_Channel_Customer2Know.SelectedItem.Text.Trim() + "', ";

            if (TextBox_Notes_Lead.Enabled)
                sSql += " Notes_Lead = '" + TextBox_Notes_Lead.Text.Trim() + "', ";
            ///else if (TextBox_Add_Notes_Lead.Enabled)
            ///{
            ///    sSql = UpdatingNotes(sSql, Response);
            ///}
            
            if (TextBox_Name_Receptionist.Enabled || bOn_Receptionist )
                sSql += " Name_Receptionist = '" + TextBox_Name_Receptionist.Text.Trim() + "', ";
                    //add_New_Name_R_F(Existing_Name_Receptionists, Session["C_N_Employee"].ToString().Trim()) + "', ";

            if (Drop_Counsellor.Enabled)/// 下拉选项有效，用TextBox更新数据库字段
                ///sSql += " Counsellor = '" + Drop_Counsellor.SelectedItem.Text.Trim() + "', ";
                sSql += " Counsellor = '" + TextBox_Counsellor.Text.Trim() + "', ";

            if (TextBox_Student_Name.Enabled)
                sSql += " Student_Name = '" + TextBox_Student_Name.Text.Trim() + "', ";

            if (TextBox_Tel_Student.Enabled)
                sSql += " Tel_Student = '" + TextBox_Tel_Student.Text.Trim() + "', ";

            if (TextBox_Date_First_Payment.Enabled)
                sSql += " Date_First_Payment = '" + TextBox_Date_First_Payment.Text.Trim() + "', ";

            if (Drop_State_Customer.Enabled)
                sSql += " State_Customer = '" + Drop_State_Customer.SelectedItem.Text.Trim() + "', ";

            if (TextBox_Series_No.Enabled)
                sSql += " Series_No = '" + TextBox_Series_No.Text.Trim() + "', ";

            if (TextBox_Name_Frontline_Agent.Enabled || bOn_Frontline_Agent )
                sSql += " Name_Frontline_Agent = '" + TextBox_Name_Frontline_Agent.Text.Trim() + "', ";
                    ///add_New_Name_R_F(Existing_Name_Frontline_Agent, Session["C_N_Employee"].ToString().Trim()) + "', ";
            /*
            if (TextBox_Date_FA_Return_Visit.Enabled)
                sSql += " Date_FA_Return_Visit = '" + TextBox_Date_FA_Return_Visit.Text.Trim() + "', ";

            if (Drop_Second_State_Customer.Enabled)
                sSql += " Second_State_Customer = '" + Drop_Second_State_Customer.SelectedItem.Text.Trim() + "', ";
            */
            sSql = sSql_Add_TextBox( TextBox_Date_FA_Return_Visit, "Date_FA_Return_Visit", sSql);
            sSql = sSql_Add_DropDownList(Drop_Second_State_Customer, null, "Second_State_Customer", sSql);
            
            if (Drop_Second_Counsellor.Enabled)/// 下拉选项有效，用TextBox更新数据库字段
                ///sSql += " Second_Counsellor = '" + Drop_Second_Counsellor.SelectedItem.Text.Trim() + "', ";
                sSql += " Second_Counsellor = '" + TextBox_Second_Counsellor.Text.Trim() + "', ";
            /*
            if (TextBox_Date_Continue_Follow_up.Enabled)
                sSql += " Date_Continue_Follow_up = '" + TextBox_Date_Continue_Follow_up.Text.Trim() + "', ";
            */
            sSql = sSql_Add_DateTime(TextBox_Date_Continue_Follow_up, "Date_Continue_Follow_up", sSql);

            sSql += " Office = '" + TextBox_Office.Text.Trim() + "' ";
            sSql += " where Series_No_Lead = '" + Label_Msg.Text.Trim() + "' ";
#if MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif
            
        }// end of Update_Lead()

        /// <summary>
        /// 添加新客户数据
        /// </summary>
        public void Insert_New_Consultation_Follow_up()
        {
            if (TextBox_Notes_Follow_up.Text.Trim() == "")
                return;

            DateTime time = DateTime.Now;

            string sSql = "insert into Consultation_Follow_up ( Series_No_Lead, Date_Operating, Name_Operator, Notes_Follow_up ) values(";
            sSql += "'" + Label_Msg.Text.Trim() + "', ";
            sSql += "'" + Common_SXD.TimeToYMD_Out( time ) + "',";
            sSql += "'" + Session["C_N_Employee"].ToString().Trim() + "',";

            sSql += "'" + Double_Single_Quotation(TextBox_Notes_Follow_up.Text.Trim()) + "' )";
#if MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif

            
            ///当前日期存入“最新回访日期”
            ///
            sSql = "Update Leads Set";

            sSql += " Date_Fresh_Return_Visit = '" + Common_SXD.TimeToYMD_Out( time ) + "' ";
            sSql += " where Series_No_Lead like '" + Label_Msg.Text.Trim() + "' ";
#if MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif
            
        }

        /// <summary>
        /// 添加新客户数据
        /// </summary>
        public void Insert_NewOne()
        {
            DateTime time = DateTime.Now;

            //	time.ToString().Trim();
            string sSql = "insert into Leads  ( /* Series_No_Lead,*/ Office, Date_Register, Date_First_Payment, Date_FA_return_visit, Date_Continue_Follow_up, Name_Inquirer, Tel_Inquirer, Email_Inquirer, QQMSN_Inquirer, Overseas_Study, Type_Contact, State_Customer, Second_State_Customer, Channel_Customer2Know, Counsellor, Name_Receptionist, Notes_Lead, Is_Active, Is_New ) values(";
            /* sSql += sSeries_No_Lead + "', "; */
            sSql += "'" + TextBox_Office.Text.Trim() + "',";
            sSql += "'" + Common_SXD.TimeToYMD_Out(TextBox_Date_Register.Text.Trim()) + "',";
            sSql += "'1900-1-1',"; //一期款付款日期
            sSql += "'1900-1-1',"; //客服回访日期
            sSql += "'1900-1-1',"; //顾问延续跟进日期
            
            sSql += "'" + TextBox_Name_Inquirer.Text.Trim() + "',";
            sSql += "'" + TextBox_Tel_Inquirer.Text.Trim() + "',";
            sSql += "'" + TextBox_Email_Inquirer.Text.Trim() + "',";
            sSql += "'" + TextBox_QQMSN_Inquirer.Text.Trim() + "',";
            sSql += "'" + Drop_Overseas_Study.SelectedItem.Text.Trim() + "', ";
            sSql += "'" + Drop_Type_Contact.SelectedItem.Text.Trim() + "', ";
            sSql += "'5星客户', ";
            sSql += "'5星客户', ";
            sSql += "'" + Drop_Channel_Customer2Know.SelectedItem.Text.Trim() + "', ";
            sSql += "'" + Drop_Counsellor.SelectedItem.Text.Trim() + "', ";

            ///sSql += "'" + TextBox_Notes_Activity.Text.Trim() + "',";
            ///sSql += "'" + TextBox_Notes_Channel_C2K.Text.Trim() + "',";
            sSql += "'" + TextBox_Name_Receptionist.Text.Trim() + "',";
//            if (TextBox_Office.Text.Trim() == "北京") /// 北京办公室，二次顾问设为“李威威”
//            {
//                sSql += "'李威威',";
//
//            }
//            else{
//                sSql += "'',";
//
//            }


            sSql += "'" + TextBox_Notes_Lead.Text.Trim() + "' , 1, 0 )";
#if MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif
        }

        public enum Func
        {
            NewOne,
            ReadOnly,
            ReplyFromCounsellor,
            ReplyFromFrontlineAgent,
            ReplyFromO_M,
            Continue_Follow_up,
            EditReceptioninst,
            EditMarketing,
            Empty
            
        }

        /// <summary>
        /// 读取功能标识
        /// </summary>
        private Func Get_Function(string strFunction)
        {
            Regex NotEditFirstPayment = new Regex("([a-zA-Z_]{1,}):([0-9]{1,})");
            Regex NewOne = new Regex("(NewOne)");
            Regex EditFirstPayment = new Regex("([0-9]{1,})");
            string[] temp;

            if (NotEditFirstPayment.IsMatch(strFunction))
            {
                temp = NotEditFirstPayment.Split(strFunction);
                if (temp[1] == "ReadOnly")
                {// 只读方式读取合同数据
                    return Func.ReadOnly;
                }
                else if (temp[1] == "ReplyFromCounsellor")
                {// 
                    return Func.ReplyFromCounsellor;
                }
                else if (temp[1] == "ReplyFromFrontlineAgent")
                {
                    return Func.ReplyFromFrontlineAgent;
                }
                else if (temp[1] == "ReplyFromO_M")
                {
                    return Func.ReplyFromO_M;
                }
                else if (temp[1] == "EditReceptionist")
                {
                    return Func.EditReceptioninst;
                }
                else if (temp[1] == "EditMarketing")
                {
                    return Func.EditMarketing;
                }
                else if (temp[1] == "Continue_Follow_up")
                {
                    return Func.Continue_Follow_up;
                }
            }
            if (NewOne.IsMatch(strFunction))
            {
                return Func.NewOne;
            }
           ///没有选择时，返回空白网页
            return Func.Empty;
        }


        /// <summary>
        /// Drop_Counsellor 控件初始化 
        /// Drop_Second_Counsellor 控件初始化 
        /// 
        /// </summary>
        private void InitializeDrop_Counsellor(string sOffice, string sSecond_Counsellor_not_display)
        {
            ///SqlConnection sqlCnn = new SqlConnection(strCnn);
            // 下载本地办公室顾问列表，绑定到 Drop_Counsellor 中
            string sSql;

                    sSql = "select * from Employees where ( office like '";
                    sSql += sOffice;
                    sSql += "' and Is_Counsellor = 1  and Is_Active = 1) or C_N_Employee like '无联系方式,无需分配' order by Code_Employee asc";

            /*
            SqlDataAdapter sqlDA = new SqlDataAdapter(sSql, sqlCnn);
            sqlDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            DataSet bsDataSet = new DataSet();
            sqlDA.Fill(bsDataSet, "Employees");
            Drop_Counsellor.DataSource = bsDataSet.Tables["Employees"];
            Drop_Counsellor.DataBind();
            sqlCnn.Close();
             */
            Common_SXD.Init_DropDownList(Drop_Counsellor, "Employees", sSql);

            sSql = "select * from Employees where ( office like '";
            sSql += sOffice;
            sSql += "' and C_N_Employee not like '";
            sSql += sSecond_Counsellor_not_display;
            sSql += "' and Is_Counsellor = 1  and Is_Active = 1) or C_N_Employee like '无联系方式,无需分配' order by Code_Employee asc";

            /*
            sqlDA = new SqlDataAdapter(sSql, sqlCnn);
            sqlDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            bsDataSet = new DataSet();
            sqlDA.Fill(bsDataSet, "Employees");
            Drop_Second_Counsellor.DataSource = bsDataSet.Tables["Employees"];
            Drop_Second_Counsellor.DataBind();
            sqlCnn.Close();
            */
            Common_SXD.Init_DropDownList(Drop_Second_Counsellor, "Employees", sSql);
        }

        /// <summary>
        ///  初始化网页显示控件，新添加的客户数据。
        /// </summary>
        private void InitControls_NewOne()
        {
            false_Enable_All_Controls();
            
            Drop_Counsellor.Enabled = true;
            TextBox_Name_Inquirer.Enabled = true;
            TextBox_Tel_Inquirer.Enabled = true;
            TextBox_Email_Inquirer.Enabled = true;
            TextBox_QQMSN_Inquirer.Enabled = true;
            TextBox_Notes_Lead.Enabled = true;
                
            Drop_Overseas_Study.Enabled = true;
            Drop_Type_Contact.Enabled = true;
            ///Drop_FromWeb.Enabled = true;
            Drop_Channel_Customer2Know.Enabled = true;
            Drop_Counsellor.Enabled = true;
            CheckBox_AllowedSameName.Enabled = true;

            Button_Refresh.Enabled = false;
            ///Button_Refresh.Visible = false;
            
        }

        /// <summary>
        ///  初始化网页显示控件，只读。
        /// </summary>
        private void InitControls_ReadOnly()
        {
            false_Enable_All_Controls();
            Bt_OK.Enabled = false;
            /*
            Drop_Counsellor.Enabled = true;
            TextBox_Name_Inquirer.Enabled = true;
            TextBox_Tel_Inquirer.Enabled = true;
            TextBox_Email_Inquirer.Enabled = true;
            TextBox_QQMSN_Inquirer.Enabled = true;
            Drop_Overseas_Study.Enabled = true;
            Drop_Type_Contact.Enabled = true;
            Drop_FromWeb.Enabled = true;
            Drop_Channel_Customer2Know.Enabled = true;
            Drop_Counsellor.Enabled = true;
            CheckBox_AllowedSameName.Enabled = true;
            */

        }

        /// <summary>
        ///  初始化网页显示控件，顾问回复。
        /// </summary>
        private void InitControls_FromCounsellor(DataRow myrowUser)
        {
            false_Enable_All_Controls();

            Session.Timeout = 118;
            string sState_Customer = myrowUser["State_Customer"].ToString().Trim();
            string sDate_FA_Return_Visit = myrowUser["Date_FA_Return_Visit"].ToString().Trim();
            System.DateTime FA_Return_Visit_DateTime = Convert.ToDateTime(sDate_FA_Return_Visit);            
            if( sState_Customer == "0无效" && 
                FA_Return_Visit_DateTime > Convert.ToDateTime("1930-1-1"))
            {
                Drop_Second_State_Customer.Enabled = true;
            }
            else
            {                
                Drop_State_Customer.Enabled = true;
            }
            ///TextBox_Add_Notes_Lead.Enabled = true;
            TextBox_Student_Name.Enabled = true;
            TextBox_Tel_Student.Enabled = true;

            TextBox_Notes_Follow_up.Enabled = true;

            string sDate_First_Payment = myrowUser["Date_First_Payment"].ToString().Trim();
            System.DateTime First_Payment_DateTime = Convert.ToDateTime(sDate_First_Payment);
            if (First_Payment_DateTime < Convert.ToDateTime("1930-1-1"))
            {/// 一期款日期为空，才能输入合同主键。
                TextBox_Series_No.Enabled = true;
            }
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            if (bIs_O_M)
                TextBox_Date_Continue_Follow_up.Enabled = true;
            else
                TextBox_Date_Continue_Follow_up.Enabled = false;
            
        }

        

        /// <summary>
        ///  初始化网页显示控件，市场专员修改数据。
        /// </summary>
        private void InitControls_EditMarketing()
        {
            false_Enable_All_Controls();


            if (Session["Email"].ToString() == "ada_xu")
            {///只允许: 许佳韵修改
                Drop_Type_Contact.Enabled = true; /// 客户接触方式
                Drop_Channel_Customer2Know.Enabled = true; /// 客户了解渠道
            }
              
            ///TextBox_Tel_Inquirer.Visible = false;
            ///TextBox_Email_Inquirer.Visible = false;
            ///TextBox_QQMSN_Inquirer.Visible = false;
            ///TextBox_Tel_Student.Visible = false;
        }

        /// <summary>
        ///  初始化网页显示控件，前台修改数据。
        /// </summary>
        private void InitControls_EditReceptionist()
        {
            false_Enable_All_Controls();

            Drop_Counsellor.Enabled = true;
            TextBox_Name_Inquirer.Enabled = true;
            TextBox_Tel_Inquirer.Enabled = true;
            TextBox_Email_Inquirer.Enabled = true;
            TextBox_QQMSN_Inquirer.Enabled = true;
            TextBox_Notes_Lead.Enabled = true;

            Drop_Overseas_Study.Enabled = true;
            Drop_Type_Contact.Enabled = true;
            ///Drop_FromWeb.Enabled = true;
            Drop_Channel_Customer2Know.Enabled = true;
            Drop_Counsellor.Enabled = true;
            CheckBox_AllowedSameName.Enabled = true;

           ///CheckBox_Is_Active.Enabled = true;
           ///
           
        }

        /// <summary>
        ///  初始化网页显示控件，客服专员回复。
        /// </summary>
        private void InitControls_FromFrontlineAgengt()
        {
            false_Enable_All_Controls();

            ///TextBox_Add_Notes_Lead.Enabled = true;
            Drop_Second_Counsellor.Enabled = true;
            TextBox_Date_FA_Return_Visit.Enabled = false;
            Drop_Second_State_Customer.Enabled = true;

            TextBox_Counsellor.Visible = true;
            Drop_Counsellor.Visible = false;
            Drop_Channel_Customer2Know.Visible = false;
            Drop_Type_Contact.Visible = false;
            TextBox_Date_Continue_Follow_up.Enabled = true;

            bOn_Frontline_Agent = true;

            TextBox_Notes_Follow_up.Enabled = true;

        }

        /// <summary>
        ///  初始化网页显示控件，地区经理回复。
        /// </summary>
        private void InitControls_FromO_M(DataRow myrowUser)
        {
            Session.Timeout = 118;
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);

            false_Enable_All_Controls();

            if(bIs_Admin)
                Drop_Channel_Customer2Know.Enabled = true;

            /// 客户状态为“0无效”时，打开修改权限
            string sState_Customer = myrowUser["State_Customer"].ToString().Trim();
            if (sState_Customer == "0无效" )
            {
                Drop_State_Customer.Enabled = true;
            }
            else
            {
                Drop_State_Customer.Enabled = false;
            }
            ///TextBox_Add_Notes_Lead.Enabled = true;
            TextBox_Date_Continue_Follow_up.Enabled = true;
            CheckBox_Is_Active.Enabled = true;

            Drop_Counsellor.Enabled = true;
            Drop_Second_Counsellor.Enabled = true;
            Drop_Second_State_Customer.Enabled = true;

            TextBox_Notes_Follow_up.Enabled = true;
        }

        /// <summary>
        ///  初始化网页显示控件，延续跟进。
        /// </summary>
        private void InitControls_Continue_Follow_up()
        {
            false_Enable_All_Controls();

            ///TextBox_Add_Notes_Lead.Enabled = true;
            TextBox_Date_Continue_Follow_up.Enabled = true;
            TextBox_Notes_Follow_up.Enabled = true;

        }

        /// <summary>
        ///  关闭所有的页面交互控制。
        /// </summary>
        private void false_Enable_All_Controls()
        {
            TextBox_Office.Enabled = false;
            TextBox_Date_Register.Enabled = false;
            TextBox_Name_Inquirer.Enabled = false;
            TextBox_Tel_Inquirer.Enabled = false;
            TextBox_Email_Inquirer.Enabled = false;
            TextBox_QQMSN_Inquirer.Enabled = false;
            Drop_Overseas_Study.Enabled = false;
            Drop_Type_Contact.Enabled = false;
            ///Drop_FromWeb.Enabled = false;
            Drop_Channel_Customer2Know.Enabled = false;
            TextBox_Notes_Lead.Enabled = false;
            ///TextBox_Add_Notes_Lead.Enabled = false;

            TextBox_Name_Receptionist.Enabled = false;
            TextBox_Counsellor.Enabled = false;
            Drop_Counsellor.Enabled = false;
            TextBox_Student_Name.Enabled = false;
            TextBox_Tel_Student.Enabled = false;
            Drop_State_Customer.Enabled = false;
            TextBox_Series_No.Enabled = false;
            TextBox_Date_First_Payment.Enabled = false;
            TextBox_Name_Frontline_Agent.Enabled = false;
            TextBox_Date_FA_Return_Visit.Enabled = false;
            Drop_Second_State_Customer.Enabled = false;
            TextBox_Second_Counsellor.Enabled = false;
            Drop_Second_Counsellor.Enabled = false;
            TextBox_Date_Continue_Follow_up.Enabled = false;
            CheckBox_Is_Active.Enabled = false;
            CheckBox_Is_New.Enabled = false;
            CheckBox_AllowedSameName.Enabled = false;
            TextBox_Notes_Follow_up.Enabled = false;

        }  
      
        
        /// <summary>
        /// 初始化，添加新的客户数据
        /// </summary>
        public void Init_NewOne()
        {
 
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            InitControls_NewOne();
            


            TextBox_Office.Text = Session["Office_Leads_Selected"].ToString().Trim();
            TextBox_Name_Receptionist.Text = Session["C_N_Employee"].ToString().Trim();

            InitializeDrop_Counsellor(TextBox_Office.Text,"");

            /// TextBox_Date_First_Payment.Text = "1900-1-1";
            /// TextBox_Date_Register.Text = "1900-1-1";
            /// TextBox_Date_FA_Return_Visit.Text = "1900-1-1";
            /// TextBox_Date_Continue_Follow_up.Text = "1900-1-1";

             
 
            DateTime time = DateTime.Now;
            TextBox_Date_Register.Text = Common_SXD.TimeToYMD(time.ToString().Trim());
/*                       TextBox_Date_Second_Receipt.Text = Common_SXD.TimeToYMD(time.ToString().Trim());

                       TextBox_Termination_Refund.Text = "0";
                       //			TextBox_First_Payment.Text = "0";
                       TextBox_Second_Payment.Text = "0";

                       if (!bIs_Counsellor)
                           TextBox_counsellor.Text = "?";
                       else
                       {
                           TextBox_counsellor.Text = Session["C_N_Employee"].ToString().Trim();
                       }
                       TextBox_Writer.Text = "?";
                       TextBox_After_Visa_US.Text = "?";
                       TextBox_Net_Applicant_US.Text = "?";

                       Common_SXD.SetSelectedIndex("?", Drop_Writer);
                       //			TextBox_Student_Name.Text = "学生";
                       //			TextBox_Pinyi.Text = "XUE Sheng";

                       Common_SXD.SetSelectedIndex("?", Drop_After_Visa_US);
                       Common_SXD.SetSelectedIndex("?", Drop_Net_Applicant_US);

                       TextBox_Date_Return_OriginalContract.Enabled = false;
            */
            Response.Write("<li>添加新客户!</li>");

              ///         Display_CheckBox_Deduct_FreeService();
     
            
        }

        /// <summary>
        /// 初始化，只读客户数据
        /// </summary>
        public void Init_ReadOnly()
        {

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            ///bool bIs_Writer = Convert.ToBoolean(Session["Is_Writer"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);
            /*
            if (!bIs_Admin )
            { //
                Response.Write("<h2 align='center'><font face='宋体' color='red'>");
                Response.Write("<br>对不起，请用其他身份登录！");
                Response.Write("</fong></h2>");
                Response.End();
            }
            */
            string sOffice = Session["Office_Leads_Selected"].ToString().Trim();
            InitializeDrop_Counsellor(sOffice, "");

            Response.Write("<li>只读数据!</li>");

            ///ReadAllFromLeads(sSql, Response);

            // 根据传入的客户序号，生成查询 SQL 语句
            string sSql = "select * from Leads where Series_No_Lead = " + "'" + sSeries_No_Lead + "'";

            DataSet bsDataSet = Init_DataSet("Leads", sSql);
            if (bsDataSet.Tables["Leads"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Leads"].Rows[0]; // 只有一行

                InitControls_ReadOnly();

                ///关闭不可见的控件
                ///

                ReadAllFromLeads(rowUser, Response);
                List_Consultation_Follow_up();

            }
            ///sqlCnn.Close();
 


        }
        /// <summary>
        /// 初始化，顾问处理客户数据
        /// </summary>
        public void Init_ReplyFromCounsellor()
        {

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            ///bool bIs_Writer = Convert.ToBoolean(Session["Is_Writer"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            
            string sOffice = Session["Office_Leads_Selected"].ToString().Trim();
            InitializeDrop_Counsellor(sOffice, "");

            Response.Write("<li>顾问处理客户数据!</li>");

            

            // 根据传入的客户序号，生成查询 SQL 语句
            string sSql = "select * from Leads where Series_No_Lead = " + "'" + sSeries_No_Lead + "'";
            
            DataSet bsDataSet = Init_DataSet("Leads", sSql);
            if (bsDataSet.Tables["Leads"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Leads"].Rows[0]; // 只有一行

                InitControls_FromCounsellor(rowUser);

                ///关闭不可见的控件
                ///



                ReadAllFromLeads(rowUser, Response);

                List_Consultation_Follow_up();

            }
            ///sqlCnn.Close();
 
        }
        /// <summary>
        /// 初始化，客服处理客户数据
        /// </summary>
        public void Init_ReplyFromFrontlineAgent()
        {

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            ///bool bIs_Writer = Convert.ToBoolean(Session["Is_Writer"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            
            DateTime time = DateTime.Now;

            Response.Write("<li>客服处理客户数据!</li>");

            // 根据传入的客户序号，生成查询 SQL 语句
            string sSql = "select * from Leads where Series_No_Lead = " + "'" + sSeries_No_Lead + "'";

            DataSet bsDataSet = Init_DataSet("Leads", sSql);
            if (bsDataSet.Tables["Leads"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Leads"].Rows[0]; // 只有一行

                string sOffice = Session["Office_Leads_Selected"].ToString().Trim();
                string sCounsellor = rowUser["Counsellor"].ToString().Trim();
                ///InitializeDrop_Counsellor(sOffice, sCounsellor);
                InitializeDrop_Counsellor(sOffice, ""); /// 二次顾问，显示全部顾问菜单
            

                InitControls_FromFrontlineAgengt();
                ///关闭不需显示的控件


                ReadAllFromLeads(rowUser, Response);

                TextBox_Date_FA_Return_Visit.Text = Common_SXD.TimeToYMD(time.ToString().Trim());

                bOn_Frontline_Agent = true;
                TextBox_Name_Frontline_Agent.Text = Common_SXD.add_New_Substring(TextBox_Name_Frontline_Agent.Text.Trim(), Session["C_N_Employee"].ToString().Trim());

                List_Consultation_Follow_up();

            }
            ///sqlCnn.Close();

        }

        /// <summary>
        /// 初始化，地区经理处理客户数据
        /// 延续跟进
        /// </summary>
        public void Init_Continue_Follow_up()
        {

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            ///bool bIs_Writer = Convert.ToBoolean(Session["Is_Writer"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);


            Response.Write("<li>延续跟进!</li>");

            // 根据传入的客户序号，生成查询 SQL 语句
            string sSql = "select * from Leads where Series_No_Lead = " + "'" + sSeries_No_Lead + "'";

            DataSet bsDataSet = Init_DataSet("Leads", sSql);
            if (bsDataSet.Tables["Leads"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Leads"].Rows[0]; // 只有一行

                InitControls_FromO_M(rowUser);

                string sOffice = Session["Office_Leads_Selected"].ToString().Trim();
                InitializeDrop_Counsellor(sOffice, "");

                DateTime time = DateTime.Now;

                ReadAllFromLeads(rowUser, Response);

                List_Consultation_Follow_up();

            }
            ///sqlCnn.Close();

        }
        
        /// <summary>
        /// 初始化，地区经理处理客户数据
        /// 延续跟进
        /// </summary>
        public void Init_ReplyFromO_M()
        {

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            ///bool bIs_Writer = Convert.ToBoolean(Session["Is_Writer"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            
            Response.Write("<li>地区经理处理客户数据!</li>");

            // 根据传入的客户序号，生成查询 SQL 语句
            string sSql = "select * from Leads where Series_No_Lead = " + "'" + sSeries_No_Lead + "'";

            DataSet bsDataSet = Init_DataSet("Leads", sSql);
            if (bsDataSet.Tables["Leads"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Leads"].Rows[0]; // 只有一行

                InitControls_FromO_M(rowUser);

                string sOffice = Session["Office_Leads_Selected"].ToString().Trim();
                InitializeDrop_Counsellor(sOffice, "");

                DateTime time = DateTime.Now;
       
                ReadAllFromLeads(rowUser, Response);

                List_Consultation_Follow_up();
            }
            ///sqlCnn.Close();


        }

        /// <summary>
        /// 初始化，前台修改客户数据
        /// </summary>
        public void Init_EditReceptionist()
        {

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Frontline_Agent"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

           
            string sOffice = Session["Office_Leads_Selected"].ToString().Trim();
            InitializeDrop_Counsellor(sOffice, "");
                        

            DateTime time = DateTime.Now;
                        Response.Write("<li>前台修改客户数据!</li>");

            // 根据传入的客户序号，生成查询 SQL 语句
            string sSql = "select * from Leads where Series_No_Lead = " + "'" + sSeries_No_Lead + "'";

            DataSet bsDataSet = Init_DataSet("Leads", sSql);

            if (bsDataSet.Tables["Leads"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Leads"].Rows[0]; // 只有一行

                InitControls_EditReceptionist();

                ReadAllFromLeads(rowUser, Response);

                bOn_Receptionist = true;
                TextBox_Name_Receptionist.Text =
                    Common_SXD.add_New_Substring(TextBox_Name_Receptionist.Text.Trim(), Session["C_N_Employee"].ToString().Trim());
            
            }
            ///sqlCnn.Close();
        }

        /// <summary>
        /// 初始化，市场专员修改数据
        /// </summary>
        public void Init_EditMarketing()
        {

            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Frontline_Agent"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            
            string sOffice = Session["Office_Leads_Selected"].ToString().Trim();
            InitializeDrop_Counsellor(sOffice, "");


            DateTime time = DateTime.Now;
            Response.Write("<li>客服专员修改客户数据!</li>");
            
            // 根据传入的客户序号，生成查询 SQL 语句
            string sSql = "select * from Leads where Series_No_Lead = " + "'" + sSeries_No_Lead + "'";

            DataSet bsDataSet = Init_DataSet("Leads", sSql);

            if (bsDataSet.Tables["Leads"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Leads"].Rows[0]; // 只有一行

                InitControls_EditMarketing();

                ReadAllFromLeads(rowUser, Response);
                /// 市场部可以看到跟进表 
                List_Consultation_Follow_up();

                bOn_Receptionist = true;
                TextBox_Name_Receptionist.Text =
                    Common_SXD.add_New_Substring(TextBox_Name_Receptionist.Text.Trim(), Session["C_N_Employee"].ToString().Trim());

            }
            ///sqlCnn.Close();
            
        }
       
        protected void Bt_OK_Click(object sender, EventArgs e)
        {
            Func eFunctions = Get_Function(Session["Flag"].ToString().Trim());
            switch (Get_Function(Session["Flag"].ToString().Trim()))
            {
                case Func.NewOne: 	// 添加一个新的客户
                    Insert_NewOne();
                    break;

                case Func.ReplyFromCounsellor:		// 顾问回复
                    /// 如果合同主键为空
                    if (TextBox_Series_No.Text.Trim() == "")
                    {
                        Update_Lead();
                        Insert_New_Consultation_Follow_up();
                        List_Consultation_Follow_up();
                    }
                    else if (TextBox_Series_No.Text.Trim() == "授权")
                    {///读取当前日期存入一期款日期
                        ///
                        TextBox_Date_First_Payment.Enabled = true;
                        TextBox_Date_First_Payment.Text = Common_SXD.TimeToYMD(DateTime.Now.ToString());
                        Update_Lead();
                        Insert_New_Consultation_Follow_up();
                        List_Consultation_Follow_up();
                        TextBox_Date_First_Payment.Enabled = false;
                        break;

                    }
                    else
                    {
                        //$ 从 contract 数据表中读取一期款日期。读取出错，拒绝修改，返回。
                        ///string str = Common_SXD.CHAR_SQL("select Date_First_Payment from Contract ",
                        ///    "Series_No like '" + TextBox_Series_No.Text.Trim() + "' and Student_Name like '" + TextBox_Student_Name.Text.Trim() + "' ", Response);
#if MySql
                        string str = Common_SXD.CHAR_MySQL("select Date_First_Payment from Contract ",
                            "Series_No like '" + TextBox_Series_No.Text.Trim() + "' and Student_Name like '" + TextBox_Student_Name.Text.Trim() + "' ", Response);
#elif MSSql
                        ///强制使用 MSSQL  数据库中的contract 表
                        ///
                        string str = Common_SXD.CHAR_MSSQL("select Date_First_Payment from Contract ",
                            "Series_No like '" + TextBox_Series_No.Text.Trim() + "' and Student_Name like '" + TextBox_Student_Name.Text.Trim() + "' ", Response);
#endif



                        if (str == "ERROR")
                        {
                            Response.Clear();
                            Response.Write("<li>合同主键、学生姓名有误， 请更正！</li>");
                            break;
                        }

                        DateTime str_DateTime = Common_SXD.YMD2Time(str);

                        if (str_DateTime < Convert.ToDateTime("1930-1-1"))
                        {
                            Response.Clear();
                            Response.Write("<li>一期款日期为空，该合同没有生效！</li>");
                            break;
                        }

                        TextBox_Date_First_Payment.Enabled = true;
                        TextBox_Date_First_Payment.Text = str;
                        Update_Lead();
                        Insert_New_Consultation_Follow_up();
                        List_Consultation_Follow_up();
                        TextBox_Date_First_Payment.Enabled = false;
                        break;
                    }


                    break;

                case Func.ReplyFromFrontlineAgent: // 客服回复
                    TextBox_Date_FA_Return_Visit.Enabled = true;
                    Update_Lead();
                    TextBox_Date_FA_Return_Visit.Enabled = false;
                    Insert_New_Consultation_Follow_up();
                    List_Consultation_Follow_up();
                    break;
                case Func.ReplyFromO_M:  // 地区经理回复
                case Func.Continue_Follow_up:  // 延续跟进
                    Update_Lead();
                    Insert_New_Consultation_Follow_up();
                    List_Consultation_Follow_up();
                    break;

                case Func.EditReceptioninst: //前台修改数据
                case Func.EditMarketing: //市场专员修改数据
                    Update_Lead();
                    break;

                case Func.ReadOnly:
                   
                    ///if (!Is_SuperUser(false))//Session["Email"].ToString().Trim() != "jianwei_rong")
                    ///    break;
                    ///else
                    ///    Update_Lead();
                    break;
                default:
                    break;

            }
        }

        /// <summary>
        /// 客户信息列表
        /// </summary>
        private void List_Consultation_Follow_up(/*string myOffice */)
        {
            ///string sSearchValue = TextBox_SearchValue.Text.Trim();

            ///string sDDList_Operator = DDList_Operator.SelectedItem.Text;
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            bool bIs_Marketing = Convert.ToBoolean(Session["Is_Marketing"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);


            DateTime dNow = DateTime.Now;

            Label_Consultation_Follow_up.Text = "已有的跟进记录：";
            
            string sSql;

            int tDay = DateTime.Now.Day;
            int tMonth = DateTime.Now.Month;
            int tYear = DateTime.Now.Year - 1;

            string sDay_Minus1Year = Convert.ToString(tYear) + "-" + Convert.ToString(tMonth) + "-" + Convert.ToString(tDay);

            ///if ( /*sDDList_Operator == "前台" && */ bIs_Receptionist)
            ///{
                Session.Timeout = 118;
                sSql = "select Series_No_Consultation_Follow_up, Date_Operating, /*Position_Operator,*/ Name_Operator, Notes_Follow_up from Consultation_Follow_up";
                sSql += " where  Series_No_Lead like '" + Label_Msg.Text.Trim()  + "' and ";
                sSql += "( Is_Active = 1  or  Is_Active is null )  ";

            ///}


            /////排序方式设定开始

            sSql += " order by Series_No_Consultation_Follow_up DESC";
            /*if (sDDList_Operator == "前台" && bIs_Receptionist)
            {
                sSql += " order by  Office, Series_No_Lead DESC";
            }
            else if (sDDList_Operator == "休眠")
            {
                sSql += " order by  Is_Active DESC, Office , State_Customer DESC, Counsellor, Series_No_Lead DESC";
            }
            else
            {
                sSql += " order by  Office , State_Customer DESC, Counsellor, Series_No_Lead DESC";
            }*/
            //////排序方式结束
             
            //$
#if MSSql
            SqlConnection sqlCnn = new SqlConnection();
            SqlDataReader sqlReader = Common_SXD.Open_MSSqlDataReader(sSql, sqlCnn, "打开数据库出错：", Response);
            int nCol = sqlReader.FieldCount;
#elif MySql
            MySqlConnection MySqlconn = new MySqlConnection();
            MySqlDataReader MysqlReader = Common_SXD.Open_MySqlDataReader(sSql, MySqlconn, "打开数据库出错：", Response);
            int nCol = MysqlReader.FieldCount;
#endif 

//            }
            //清除表中的内容
            Table_Consultation_Follow_up.Rows.Clear();

            //首先生成标题行
            {
                HtmlTableRow row = new HtmlTableRow();
                for (int i = 0; i < nCol; i++)
                {
                    // Create a new cell and add it to the Cells collection.
                    HtmlTableCell cell = new HtmlTableCell();
                    ///string temp = "";
#if MSSql
                    string temp = Common_SXD.English_Chinese(sqlReader.GetName(i));
#elif MySql
                    string temp = Common_SXD.English_Chinese(MysqlReader.GetName(i));

#endif
                    ////////////////////
               /*     switch (Common_SXD.get_Flag_Database())
                    {
                        case Flag_Database.MSSql:
                            {
                                temp = Common_SXD.English_Chinese(sqlReader.GetName(i));
                                break;
                            }
                        case Flag_Database.MySql:
                            {
                                temp = Common_SXD.English_Chinese(MysqlReader.GetName(i));
                                break;
                            }

                    }
                    *//////////////////////////////
                    cell.Controls.Add(new LiteralControl(temp.Trim()));
                    row.Cells.Add(cell);
                }
                
                Table_Consultation_Follow_up.Rows.Add(row);
                row.BgColor = "Tan";
            }

            // 依次读出数据
            int j = 0;
/*            bool bRead = false;
            switch (Common_SXD.get_Flag_Database())
            {
                case Flag_Database.MSSql:
                    {
                        bRead = sqlReader.Read();
                        break;
                    }
                case Flag_Database.MySql:
                    {
                        bRead = MysqlReader.Read();
                        break;
                    }

            }*/
#if MSSql
            while (sqlReader.Read())
#elif MySql
            while (MysqlReader.Read())
#endif
            {
                HtmlTableRow row = new HtmlTableRow();
#if MSSql
                String sNo_Series_Lead = sqlReader.GetInt64(0).ToString().Trim();
#elif MySql
                String sNo_Series_Lead = MysqlReader.GetInt64(0).ToString().Trim();
#endif

                ////////////////////////////////
                // Provide a different background color for alternating rows.
                if (j % 2 == 1)
                    row.BgColor = "Gainsboro";

                // Iterate through the cells of a row.
                {
                    HtmlTableCell cell = new HtmlTableCell();
                    /*
                    cell.Controls.Add(new LiteralControl("<a href=\"JavaScript:comOpen('./LeadEdit.aspx?Series_No_Lead=ReadOnly:" +
                        sNo_Series_Lead.Trim() +
                        "',900,620);\">只读" +
                        sNo_Series_Lead.Trim() +
                        "</a>"));*/
                    cell.Controls.Add(new LiteralControl("<a>" +
                        sNo_Series_Lead.Trim() +
                        "</a>"));

                    row.Cells.Add(cell);
                }

                for (int i = 1; i < nCol; i++)
                {
                    // Create a new cell and add it to the Cells collection.
                    HtmlTableCell cell = new HtmlTableCell();

                    /*
                    switch (Common_SXD.get_Flag_Database())
                    {
                        case Flag_Database.MSSql:
                            {
                                cell.Controls.Add(new LiteralControl(Common_SXD.TimeToYMD(sqlReader.GetValue(i).ToString())));
                                break;
                            }
                        case Flag_Database.MySql:
                            {
                                cell.Controls.Add(new LiteralControl(Common_SXD.TimeToYMD(MysqlReader.GetValue(i).ToString())));
                                break;
                            }

                    }
                     */
#if MSSql
                    cell.Controls.Add(new LiteralControl(Common_SXD.TimeToYMD(sqlReader.GetValue(i).ToString())));
#elif MySql
                    cell.Controls.Add(new LiteralControl(Common_SXD.TimeToYMD(MysqlReader.GetValue(i).ToString())));
#endif
                    row.Cells.Add(cell);
                }


                Table_Consultation_Follow_up.Rows.Add(row);
                j++;
                /*
                switch (Common_SXD.get_Flag_Database())
                {
                    case Flag_Database.MSSql:
                        {
                            bRead = sqlReader.Read();
                            break;
                        }
                    case Flag_Database.MySql:
                        {
                            bRead = MysqlReader.Read();
                            break;
                        }

                }*/
            }
#if MSSql
            sqlReader.Close(); // 关闭sqDataReader
            sqlCnn.Close(); // 关闭数据库连接
#elif MySql
            MysqlReader.Close(); // 关闭sqDataReader
            MySqlconn.Close(); // 关闭数据库连接
            
#endif

        }

        protected void Drop_Counsellor_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox_Counsellor.Text = Drop_Counsellor.SelectedItem.Text.Trim();
        }

        protected void Drop_Second_Counsellor_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox_Second_Counsellor.Text = Drop_Second_Counsellor.SelectedItem.Text.Trim();
        }

        protected void Button_Refresh_Click(object sender, EventArgs e)
        {
            Func eFunctions = Get_Function(Session["Flag"].ToString().Trim());
            switch (Get_Function(Session["Flag"].ToString().Trim()))
            {
                case Func.NewOne:
                    break;

                default:
                    List_Consultation_Follow_up();
                    break;

            }

        }

        
    }
}
